


	import java.awt.*;
	 
	import javax.swing.*;
	 
	public class GridBagLayoutBeispiel extends JLayeredPane{
	    public static void main( String[] args ) throws Exception{
	        UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
	        GridBagLayoutBeispiel panel = new GridBagLayoutBeispiel();
	        
	        JFrame frame = new JFrame( "GridBagLayout" );
	        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
	        frame.add( panel );
	        
	        JButton top = new JButton( "top" );
	        GridBagConstraints topConstraints = new GridBagConstraints();
	        topConstraints.gridx = 2;
	        topConstraints.gridy = 5;
	        topConstraints.gridwidth = 3;
	        topConstraints.gridheight = 1;
	        
	        JButton bottomLeft = new JButton( "bottom left" );
	        GridBagConstraints bottomLeftConstraints = new GridBagConstraints();
	        bottomLeftConstraints.gridx = 0;
	        bottomLeftConstraints.gridy = 1;
	        bottomLeftConstraints.gridwidth = 1;
	        bottomLeftConstraints.gridheight = 1;
	        
	        JButton bottomRight = new JButton("a very very long and useless text" );
	        GridBagConstraints bottomRightConstraints = new GridBagConstraints();
	        bottomRightConstraints.gridx = 0;
	        bottomRightConstraints.gridy = 2;
	        bottomRightConstraints.gridwidth = 1;
	        bottomRightConstraints.gridheight = 1;
	        
	        panel.add( top, topConstraints );
	        panel.add( bottomLeft, bottomLeftConstraints );
	        panel.add( bottomRight, bottomRightConstraints );
	        
	        frame.pack();
	        frame.setLocationRelativeTo( null );
	        frame.setVisible( true );
	    }
	    
	    /*
	     * Alles was jetzt noch folgt ist lediglich dazu da, das Gitter welches
	     * vom GridBagLayout benutzt wird, aufzuzeichnen.
	     */
	    private JPanel content;
	    private JComponent glass;
	    private GridBagLayout layout;
	    
	    public GridBagLayoutBeispiel(){
	        setLayout( new OverlayLayout( this ) );
	 
	        layout = new GridBagLayout();
	        content = new JPanel( layout );
	        content.setBorder( BorderFactory.createEmptyBorder( 2, 2, 2, 2 ) );
	        content.setOpaque( true );
	        content.setBackground( Color.WHITE );
	        
	        glass = new JComponent(){
	            @Override
	            protected void paintComponent( Graphics g ) {
	                Point origin = layout.getLayoutOrigin();
	                int[][] dimensions = layout.getLayoutDimensions();
	                g = g.create();
	                
	                g.setColor( Color.BLACK );
	                ((Graphics2D)g).setStroke( new BasicStroke( 2f ) );
	                
	                int x = origin.x;
	                int y = origin.y;
	                int w = 0;
	                int h = 0;
	                
	                for( int i = 0, n = dimensions[0].length; i<n; i++ )
	                    w += dimensions[0][i];
	                for( int i = 0, n = dimensions[1].length; i<n; i++ )
	                    h += dimensions[1][i];
	                
	                int tx = x;
	                int ty = y;
	                for( int i = 0, n = dimensions[0].length; i<=n; i++ ){
	                    g.drawLine( tx, y, tx, y+h );
	                    if( i<n )
	                        tx += dimensions[0][i];
	                }
	                
	                for( int i = 0, n = dimensions[1].length; i<=n; i++ ){
	                    g.drawLine( x, ty, x+w, ty );
	                    if(i<n)
	                        ty += dimensions[1][i];
	                }
	                
	                g.dispose();
	            }
	            @Override
	            public boolean contains( int x, int y ) {
	                return false;
	            }
	        };
	        glass.setOpaque( false );
	        
	        super.add( content );
	        setLayer( content, DEFAULT_LAYER );
	        
	        super.add( glass );
	        setLayer( glass, MODAL_LAYER );
	    }
	    
	    @Override
	    public void add( Component component, Object constraints ){
	        content.add( component, constraints );
	    }
	}



